﻿using System;
using System.Text;
using System.Windows.Forms;
using iTunesExport.Parser;
using System.IO;

namespace iTunesUtil
{   
    public partial class CheckITunesFiles : Form
    {
        bool bStop;
        string msg;
        string fullMsg;
        readonly TrackCollection tracks;
        TrackCollection missingTracks;
        MessageBoxIcon icon = MessageBoxIcon.Information;
        
        public TrackCollection MissingTracks
        {
            get { return  missingTracks; }
        }
        
        public string Info { get { return fullMsg; } }

        public CheckITunesFiles(TrackCollection tracks)
        {
            InitializeComponent();                        
            missingTracks = new TrackCollection(string.Format("Analysing iTunes files at {0}", DateTime.Now.ToShortTimeString()), "Missing iTunes tracks");         
            this.tracks = tracks;
            Text = string.Format("Analysing {0} tracks from iTunes library", tracks.Count);
            progressBar1.Maximum = tracks.Count;        
            new Action(CheckItunes).BeginInvoke((iar => Invoke(new MethodInvoker(delegate{
                    MessageBox.Show(msg, Text,MessageBoxButtons.OK, icon);
                    Close();
                }))), null);
        }

        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        private void CheckItunes()
        {
            int max = tracks.Count;
            int value = 0;
            int count = max / 100;
            foreach (Track currTrack in tracks.Tracks)
            {
                if (bStop)
                    break;

                if (++value % count == 0)
                    ShowProgress(value, max);
                
                if (currTrack.Location != null && File.Exists(currTrack.Location))                    
                    continue;
                
                missingTracks.Add(currTrack);                
            }
            ShowProgress(value, max);
            if (value == max)
                msg = string.Format("All {0} files checked, {1} missing tracks !", value, missingTracks.Count);
            else
            {
                icon = MessageBoxIcon.Warning;
                msg = string.Format("{0} from {1} files checked, {2} missing tracks !", value, max, missingTracks.Count);
            }
            var sb = new StringBuilder();
            sb.AppendLine(msg);
            sb.AppendLine("Similar artist names");
            sb.AppendLine("====================");
            sb.AppendLine(MainForm.CheckSimilarArtistNames());
            sb.AppendLine("Similar track names");
            sb.AppendLine("===================");            
            sb.AppendLine(MainForm.CheckSimilarTrackNames());
            sb.AppendLine("Tracks with equal file size");
            sb.AppendLine("===================");            
            sb.AppendLine(MainForm.CheckSimilarTracksByFileSize());
            fullMsg = sb.ToString();
        }

        private void OnButtonStop(object sender, EventArgs e)
        {
            bStop = true;
        }

        private void ShowProgress(int value, int max)
        {
            if (InvokeRequired == false)
            {
                progressBar1.Value = value;                
                label1.Text = string.Format("{0}/{1} {2}%", value, max, (value * 100) / max);
                return;
            }
            Invoke(new ShowProgressDelegate(ShowProgress), new object[] { value, max });
        }
    }
}
